﻿Imports System.Collections

''' <summary>
''' This is a game component that implements IUpdateable.
''' </summary>

Public Class SpriteManager
    Inherits Microsoft.Xna.Framework.GameComponent

    Public WithEvents Player As Sprite
    Public SpriteList As New ArrayList

    Public Sub New(ByVal Game As DanoQuest)
        MyBase.New(Game)
        Me.Game.Components.Add(Me)

        ' TODO: Construct any child components here
    End Sub

    ''' <summary>
    ''' Allows the game component to perform any initialization it needs to before starting
    ''' to run.  This is where it can query for any required services and load content.
    ''' </summary>
    Public Overrides Sub Initialize()
        ' TODO: Add your initialization code here
        MyBase.Initialize()
    End Sub

    ''' <summary>
    ''' Allows the game component to update itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Public Overrides Sub Update(ByVal gameTime As GameTime)
        If (Player.isAttacking = True Or Player.Target.isAttacking = True) And Player.IsDead = False And Player.Target.IsDead = False Then
            Player.Stats.HasTarget = True
            Player.Attack(Player.Target)
            Player.Target.Attack(Player)
        ElseIf Player.Target.IsDead = True Then
            Player.Stats.EXP += Player.Target.Stats.Level * 10
            Player.isAttacking = False 'Todo: Handle combat flags in another way
            Player.Target.isAttacking = False
            Kill(Player.Target) 'Get next target from SpriteManager
            Player.Stats.HasTarget = False
            Player.Target = Player
        End If
        MyBase.Update(gameTime)
    End Sub

    Public Sub Add(ByRef Sprite As Sprite)
        Sprite.IsDead = False
        SpriteList.Add(Sprite)
    End Sub

    Public Sub AddPlayer(ByVal Player As Sprite)
        Player.IsPlayer = True
        SpriteList.Add(Player)
        Me.Player = Player
    End Sub

    Public Sub GetNext(ByRef Target As Sprite)
        If SpriteList.Count > 1 Then
            Player.Target = DirectCast(SpriteList.Item(1), Sprite) 'Todo:
        Else
            Player.Target = DirectCast(SpriteList.Item(0), Sprite) 'Target self
        End If
    End Sub

    Public Sub Kill(ByRef Sprite As Sprite)
        Sprite.IsDead = True
        Game.Components.OfType(Of SpawnManager).ToList.Item(0).ResetTimer(Sprite)
        SpriteList.Remove(Sprite)
    End Sub

End Class
